package testing;

import static org.junit.Assert.*;

import java.awt.List;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import lcs.Lcs;

import org.junit.Test;

import document.Document;
import tree.Node;
import tree.Tree;

public class MainTest {

	@Test
	public void buildTreeKamusBahasaIndonesia() {
		try {
			/**
			 * open jar sebagai helper masuk jdbc
			 */
			Class.forName("com.mysql.jdbc.Driver");

			/**
			 * membangun koneksi ke url tersebut
			 */
			Connection connection = DriverManager
					.getConnection("jdbc:mysql://localhost/finalproject_web?user=root");

			/**
			 * objek unuk dieksekusi
			 */
			Statement statement = connection.createStatement();

			/**
			 * eksekusi query -> disimpan di result
			 */
			ResultSet result = statement.executeQuery("select kata from token");

			/**
			 * ambil objek pertama
			 */
			result.next();

			/**
			 * masukkan kata pertama -> nge-construct tree
			 */
			Tree tree = new Tree(new Node(result.getString("kata").hashCode()));

			/**
			 * selama result masih ada, masukan result ke tree
			 */
			while (result.next()) {
				tree.insert(result.getString("kata").hashCode());
			}

			/**
			 * save ke file
			 */
			Tree.save(tree, "kamusBahasaIndonesia.ich");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void buildTreeStopWord() {
		Document temp = new Document(787, "liststopword.txt");	
		/**
		 * masukkan kata pertama -> nge-construct tree
		 */
		Tree tree = new Tree(new Node(temp.getWords().get(0).hashCode()));

		/**
		 * selama result masih ada, masukan result ke tree
		 */
		for (String words : temp.getWords()){
			tree.insert(words.hashCode());
		}	

		/**
		 * save ke file
		 */
		Tree.save(tree, "stopWord.ich");
		tree.print(tree.getRoot());
		System.out.print(tree.getJumVal());
	}

	@Test
	public void testLCSFileFolder() {
		/**
		 * buka folder dimasukkan ke testFile
		 */
		File testFile = new File(
				"C:/Users/channisa/workspace/tugas-akhir-113100025/TugasAkhirIcha/dataSetMedium");

		/**
		 * variabel penampung list file
		 */
		java.util.List<Document> listDokumen = new ArrayList<Document>();
		/**
		 * looping di folder tersebut, dimasukkan ke file
		 */
		for (File file : testFile.listFiles()) {

			/**
			 * jika itu file, dimasukkan ke variabel d
			 */
			if (file.isFile()) {

				/**
				 * c=onstruct d
				 */
				Document d = new Document(666, file.getPath());

				/**
				 * add dokumen ke listDokuen
				 */
				listDokumen.add(d);

				/**
				 * Print judul dokumen
				 */
				// System.out.println(file.getName());

				/**
				 * looping buat print isi file
				 */
				// for (int i = 0; i < d.getWords().size(); i++) {
				// System.out.print(d.getWords().get(i)+" ");
				// }
				// System.out.println();
				// System.out.println();
			}
		}

		/**
		 * looping untuk melakukan LCS tiap file
		 */
		for (int i = 0; i < listDokumen.size() - 1; i++) {
			Document query = listDokumen.get(i);
			for (int j = i + 1; j < listDokumen.size(); j++) {
				System.out.println("Dokumen " + (i + 1) + " vs Dokumen "
						+ (j + 1));
				Lcs lcsTest = new Lcs(query.getWords(), listDokumen.get(j)
						.getWords());
				// lcsTest.print_matrix_lcs();
				// lcsTest.print_matrix_path();
				// System.out.println();
			}
		}

	}

}
